package com.dd.cloud.user.service.resource.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.ResourceConstants;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.utils.ObjectUtils;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.resource.PostInfo;
import com.dd.cloud.user.mapper.resource.PostInfoMapper;
import com.dd.cloud.user.req.resource.PostInfoCreateReq;
import com.dd.cloud.user.req.resource.PostInfoPageReq;
import com.dd.cloud.user.req.resource.PostInfoPageSysReq;
import com.dd.cloud.user.req.resource.PostInfoUpdateReq;
import com.dd.cloud.user.res.resource.PostInfoDetailH5Res;
import com.dd.cloud.user.res.resource.PostInfoPageRes;
import com.dd.cloud.user.res.resource.PostInfoPageSysRes;
import com.dd.cloud.user.service.resource.IPostInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author Lx
 * @version 1.0
 * @date 2023/10/16
 */
@Slf4j
@Service
public class PostInfoServiceImpl extends ServiceImpl<PostInfoMapper, PostInfo> implements IPostInfoService {
    /**
     * 创建帖子/发帖 运营后台
     *
     * @param req
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createPostInfoSys(PostInfoCreateReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        PostInfo postInfo = new PostInfo();
        BeanUtils.copyProperties(req, postInfo);
        postInfo.setStatus(ResourceConstants.PostInfoStatus.NORMAL);
        postInfo.setCreateId(loginInfo.getId());
        postInfo.setCreateType(loginInfo.getType());
        postInfo.setCreateDate(LocalDateTime.now());
        save(postInfo);
    }

    /**
     * 修改帖子 运营后台
     *
     * @param req
     */
    @Override
    public void updatePostInfoSys(PostInfoUpdateReq req) {
        UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        PostInfo one = getById(req.getId());
        if (one == null) {
            throw new ZekeException(400, "帖子不存在");
        }
        PostInfo postInfo = new PostInfo();
        BeanUtils.copyProperties(req, postInfo);
        updateById(postInfo);
    }

    /**
     * 置顶帖子
     *
     * @param id 帖子id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void toppingPostInfo(Integer id) {
        PostInfo postInfo = getById(id);
        if (postInfo == null || postInfo.getStatus() == ResourceConstants.PostInfoStatus.DEL) {
            throw new ZekeException(400, "帖子不存在/或已被删除");
        }
        int sortIndex;
        QueryWrapper<PostInfo> qw = new QueryWrapper<>();
        qw.lambda().isNotNull(PostInfo::getSort);
        qw.lambda().orderByDesc(PostInfo::getSort);
        qw.last("limit 1");
        PostInfo sort = getOne(qw);
        if (sort == null) {
            sortIndex = 1;
        } else {
            sortIndex = sort.getSort() + 1;
        }
        UpdateWrapper<PostInfo> uw = new UpdateWrapper<>();
        uw.lambda().eq(PostInfo::getId, id);
        uw.lambda().set(PostInfo::getSort, sortIndex);
        update(uw);
    }

    /**
     * 取消置顶帖子
     *
     * @param id 帖子id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelToppingPostInfo(Integer id) {
        PostInfo postInfo = getById(id);
        if (postInfo == null || postInfo.getStatus() == ResourceConstants.PostInfoStatus.DEL) {
            throw new ZekeException(400, "帖子不存在/或已被删除");
        }
        UpdateWrapper<PostInfo> uw = new UpdateWrapper<>();
        uw.lambda().eq(PostInfo::getId, id);
        uw.lambda().set(PostInfo::getSort, null);
        update(uw);
    }

    /**
     * 修改帖子状态
     *
     * @param id
     * @param status
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePostInfoStatus(Integer id, Integer status) {
        if (!ObjectUtils.refrect(ResourceConstants.PostInfoStatus.class, status)) {
            throw new ZekeException(ZkExceptionEnum.STATUS_DATA_ERROR);
        }
        PostInfo postInfo = getById(id);
        if (postInfo == null) {
            throw new ZekeException(400, "帖子信息不存在");
        }
        UpdateWrapper<PostInfo> uw = new UpdateWrapper<>();
        uw.lambda().eq(PostInfo::getId, id);
        uw.lambda().set(PostInfo::getStatus, status);
        update(uw);
    }

    /**
     * 查询帖子列表-分页-运营后台
     *
     * @param req
     * @return
     */
    @Override
    public IPage<PostInfoPageSysRes> queryPostInfoPageSys(PostInfoPageSysReq req) {
        IPage<PostInfoPageSysRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryPostInfoPageSys(iPage, req);
        return iPage;
    }

    /**
     * 查询帖子列表-分页-医生端
     *
     * @param req
     * @return
     */
    @Override
    public IPage<PostInfoPageRes> queryPostInfoPage(PostInfoPageReq req) {
        if (req.getDocId() == null) {
            LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.DOCTOR).get();
            req.setDocId(loginInfo.getId());
        }
        IPage<PostInfoPageRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryPostInfoPage(iPage, req);
        return iPage;
    }

    /**
     * 查询帖子详情-h5(目前可共用所有查询帖子详情数据)
     *
     * @param id
     * @return
     */
    @Override
    public PostInfoDetailH5Res queryPostInfoDetailH5(Integer id) {
        return getBaseMapper().queryPostInfoDetailH5(id);
    }
}
